package net.lainiao;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.transaction.CuratorTransaction;
import org.apache.curator.framework.api.transaction.CuratorTransactionFinal;
import org.apache.curator.framework.api.transaction.CuratorTransactionResult;

import java.util.Collection;

/**
 * Created by Administrator on 2016/11/10.
 */
public class TransactionExample {
    public static final String PATH="/TransactionExample/path";
    public static void main(String[] args) {

    }

    public static Collection<CuratorTransactionResult> transactionResults(CuratorFramework client) throws Exception {
        Collection<CuratorTransactionResult> results=client.inTransaction().create().forPath("/a/path","some date".getBytes()).and().setData().forPath("/t/path1","other data".getBytes()).and().delete().forPath("/yet/another/path").and().commit();
        for (CuratorTransactionResult result:results){
            System.out.println(result.getForPath()+"-"+result.getType());
        }
        return results;
    }

    public static CuratorTransaction startTransaction(CuratorFramework client){
        return client.inTransaction();
    }

    public static CuratorTransactionFinal addCreateTransactionFinal(CuratorTransaction transaction) throws Exception {
        return transaction.create().forPath("/a/path","some data".getBytes()).and();
    }

    public static CuratorTransactionFinal addDeleteToTransaction(CuratorTransaction transaction) throws Exception {
        return transaction.delete().forPath("/another/path").and();
    }

    public static void commitTransaction(CuratorTransactionFinal transactionFinal) throws Exception {
        transactionFinal.commit();
    }
}
